Skip to content

Conversation

yondifon
Copy link
Contributor

This update adds modifiers to filter classes in architectural tests by their traits, interfaces, and inheritance, enabling more precise

  • extending(): Filters classes that extend a specific
  • implementing(): Filters classes that implement a specific interface
  • usingTrait(): Filters classes that directly use a specific trait

e.g.
// Ensure that all Laravel Jobs implementing ShouldQueue use the InteractsWithQueue and Batchable traits

expect('App\Jobs')
    ->classes()
    ->implementing(ShouldQueue::class)
    ->toUseTrait(InteractsWithQueue::class)

@Lukasss93
Copy link
Contributor

Lukasss93 commented Mar 27, 2025

It would be cool to have that as well:

public function withMethod(string $filterMethod): self
{
    $this->excludeCallbacks[] = function (ObjectDescription $object) use ($filterMethod): bool  {
        foreach($object->methods as $method) {
            if ($method->name === $filterMethod) {
                return false;
            }
        }

        return true;
    };

    return $this;
}

I would need it on a folder with many files, but I want to filter only the classes that have a certain method.

An example of one of my use cases:

arch('check types with static make method does not have constructor')
    ->expect('MyApp\Store\Types') // 200+ files
    ->classes()
    ->withMethod('make') // 50~ files
    ->not->toHaveConstructor();

PS: But is this project still maintained?

@nunomaduro nunomaduro merged commit f93a60c into pestphp:3.x Mar 30, 2025
@nunomaduro
Copy link
Member

Can you make a pull request to the docs please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants